<template>
  <a-form-model-item :key="index" :label="item['@attributes'].name" :required="getDevTipAndRequired(item.rules)[1]">
    <slot></slot>
    <div class="ts" v-if="getDevTipAndRequired(item.rules)[0]">
      {{ getDevTipAndRequired(item.rules)[0] }}
    </div>
  </a-form-model-item>
</template>

<script>
export default {
  props: {
    item: {
      type: Object,
      default() {
        return null
      }
    }
  },

  computed: {
    getDevTipAndRequired() {
      return rules => {
        let newArr = JSON.parse(JSON.stringify(rules.rule))
        if (!Array.isArray(newArr)) {
          newArr = [newArr]
        }

        let devTip = newArr.find(el => el['@attributes'].type === 'devTip'), // 提示信息
          required = newArr.find(el => el['@attributes'].type === 'required') // 是否必填
        return [
          devTip ? devTip['@attributes'].value : '',
          required && required['@attributes'].value === 'true' ? true : false
        ]
      }
    }
  }
}
</script>

<style lang="less" scoped>
.tip {
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
  line-height: normal;
}
</style>
